#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@Author   : long.hai
@Contact  : 1982964634@qq.com
"""
from typing import List, Dict, Any


class Action:
    def __init__(self, d: Dict[str, Any]):
        self.is_element = d.get('is_element')
        self.is_params = d.get('is_params')
        self.is_return = d.get('is_return')
        self.method = d.get('method', '')

    def __repr__(self):
        return f"<Action({self.method})>"


class Locator:
    def __init__(self, d: Dict[str, Any]):
        self.by = d.get('by', '')
        self.params = d.get('params', '')

    def __repr__(self):
        return f"<Locator({self.by}, {self.params})>"


class LocatorList:
    def __init__(self, d: List[Dict[str, Any]]):
        if d:
            self.children = [Locator(locator) for locator in d]
        else:
            self.children = []

    def __repr__(self):
        return f"<LocatorList({len(self.children)} items)>"


class Config:
    def __init__(self, d: Dict[str, Any]):
        self.before_wait = d.get('before_wait', 0)
        self.timeout = d.get('timeout', 5)
        self.intelligence_wait = d.get('intelligence_wait', 1)
        self.ignore_error = d.get('ignore_error', 1)
        self.after_screenshot = d.get('after_screenshot', 1)

    def __repr__(self):
        return f"<Config({self.timeout}, {self.ignore_error})>"


class Step:
    def __init__(self, d: Dict[str, Any]):
        self.id = d.get('id')
        self.uuid = d.get('uuid', '')
        self.name = d.get('name', '')
        self.rate = d.get('rate', 0)
        self.case_id = d.get('case_id')
        self.action_id = d.get('action_id')
        self.element_id = d.get('element_id')
        self.params = d.get('params', '')
        self.result = d.get('result', '')
        self.enable = d.get('enable')

        self.action = Action(d.get('action', {}))
        self.config = Config(d.get('config', {}))
        self.locators = LocatorList(d.get('locators', []))

        if "children" in d:
            self.children = [Step(step) for step in d.get('children', [])]

    def __repr__(self):
        return f"<Step({self.name}, {self.action})>"


class StepList:
    def __init__(self, d: List[Dict[str, Any]]):
        if d:
            self.children = [Step(item) for item in d]
        else:
            self.children = []

    def __repr__(self):
        return f"<StepList({len(self.children)} items)>"


class TestCase:
    def __init__(self, d: Dict[str, Any]):
        self.id = d.get('id', 0)
        self.name = d.get('name', '')
        self.group_id = d.get('group_id')
        self.type = d.get('type')
        self.tag = d.get('tag', [])
        self.config = d.get('config', '')
        self.variables = d.get('variables', [])
        self.steps: StepList = StepList(d.get('steps', []))

    def __repr__(self):
        return f"<TestCase({self.name})>"


class TestCaseList:
    def __init__(self, d: List[Dict[str, Any]]):
        if d:
            self.children = [TestCase(item) for item in d]
        else:
            self.children = []

    def __repr__(self):
        return f"<TestCaseList({len(self.children)} items)>"
